package com.shanling.shanlingcontroller.ui.activity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.shanling.shanlingcontroller.R;
import com.shanling.shanlingcontroller.adapter.CommonAdapter;
import com.shanling.shanlingcontroller.adapter.ViewHolder;
import com.shanling.shanlingcontroller.base.BaseAppCompatActivity;
import com.shanling.shanlingcontroller.bean.Constants;
import com.shanling.shanlingcontroller.bean.EventCenter;
import com.shanling.shanlingcontroller.bean.Music;
import com.shanling.shanlingcontroller.bean.MyMusic;
import com.shanling.shanlingcontroller.music.player.PlaylistLoader;
import com.shanling.shanlingcontroller.utils.ConvertUtils;
import com.shanling.shanlingcontroller.utils.CoverLoader;
import com.shanling.shanlingcontroller.utils.ToastUtils;
import com.shanling.shanlingcontroller.view.MyDilalogUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class MusicsOperatingActivity extends BaseAppCompatActivity implements AdapterView.OnItemClickListener, MyDilalogUtils.OnDialogClickListener {


    @BindView(R.id.tv_back)
    TextView tvBack;
    @BindView(R.id.lv_song)
    ListView lvSong;
    @BindView(R.id.ll_add)
    LinearLayout llAdd;
    @BindView(R.id.ll_delete)
    LinearLayout llDelete;
    @BindView(R.id.tv_all)
    TextView tvAll;
    private  boolean isCheck=false;
    private List<MyMusic> musicList = new ArrayList<>();
    private CommonAdapter<MyMusic> myAdapter;
    private MyDilalogUtils dilalogUtils;
    private  List<Music> checkMusics=new ArrayList<>();

    @Override
    protected void getBundleExtras(Bundle extras) {
        List<Music> list = extras.getParcelableArrayList("musicList");
        for (int i = 0; i < list.size(); i++) {
            musicList.add(new MyMusic(false, list.get(i)));
        }
    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.activity_musics_operating;
    }

    @Override
    protected void initViewsAndEvents() {
        myAdapter = new CommonAdapter<MyMusic>(this, musicList, R.layout.item_music) {
            @Override
            public void convert(ViewHolder helper, int position, MyMusic item) {
                ImageView iv_selectot = helper.getView(R.id.iv_selector);
                ImageView iv_cover=helper.getView(R.id.iv_ablum);
                TextView tv_song = helper.getView(R.id.tv_song_name);
                TextView tv_singer = helper.getView(R.id.tv_singer);
                if (item.isSelected()) {
                    iv_selectot.setImageResource(R.drawable.nav_icon_more_n);
                } else {
                    iv_selectot.setImageResource(R.drawable.music_batch_operation_icon_default);
                }
                CoverLoader.INSTANCE.loadImageView(mContext, item.getMusic().getCoverUri(), iv_cover);
                tv_song.setText(ConvertUtils.getTitle(item.getMusic().getTitle()));
                tv_singer.setText(ConvertUtils.getArtistAndAlbum(item.getMusic().getArtist(), item.getMusic().getAlbum()));
            }
        };
        lvSong.setAdapter(myAdapter);
        lvSong.setOnItemClickListener(this);
        dilalogUtils = new MyDilalogUtils(this, R.style.myDialog,getString(R.string.Delete), getString(R.string.Confirm_delete));
        dilalogUtils.setOnDialogClickListener(this);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected boolean isBindEventBusHere() {
        return false;
    }

    @Override
    protected void onEventComing(EventCenter eventCenter) {

    }

    @Override
    protected boolean toggleOverridePendingTransition() {
        return false;
    }

    @Override
    protected TransitionMode getOverridePendingTransitionMode() {
        return null;
    }


    @OnClick({R.id.tv_back, R.id.ll_add, R.id.ll_delete,R.id.tv_all})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_back:
                finish();
                break;
            case R.id.ll_add:
                getCheckMusics();
                if(checkMusics.size()==0){
                    ToastUtils.showToast(this,R.string.select_thesong);
                    return;
                }
                boolean isAdd = PlaylistLoader.INSTANCE.addMusicList(Constants.PLAYLIST_QUEUE_ID, checkMusics);
                if(isAdd){
                    ToastUtils.showToast(this,R.string.Added_successfully);
                    finish();
                }else {
                    ToastUtils.showToast(this,R.string.Add_failed);
                }
                break;
            case R.id.ll_delete:
                getCheckMusics();
                if(checkMusics.size()==0){
                    ToastUtils.showToast(this,R.string.select_thesong);
                    return;
                }
                dilalogUtils.show();
                dilalogUtils.setCancelable(false);
                break;
            case R.id.tv_all:
                if(isCheck){
                    tvAll.setText(getString(R.string.select_all));
                    setCheck(false);
                }else {
                    tvAll.setText(getString(R.string.unselect_all));
                    setCheck(true);
                }
                isCheck=!isCheck;
                break;
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        boolean selected = musicList.get(position).isSelected();
        musicList.get(position).setSelected(!selected);
        myAdapter.refresh(musicList);
    }

    private  void setCheck(boolean isCheck){
        for (int i = 0; i < musicList.size(); i++) {
            musicList.get(i).setSelected(isCheck);
        }
        myAdapter.refresh(musicList);
    }
    @Override
    public void onSure() {
       PlaylistLoader.INSTANCE.deleteLocalMusic(this,checkMusics,null);
        finish();
    }
    @Override
    public void onCancel() {

    }

    private void getCheckMusics(){
        checkMusics.clear();
        for (int i = 0; i <musicList.size() ; i++) {
            if(musicList.get(i).isSelected()){
                checkMusics.add(musicList.get(i).getMusic());
            }
        }
    }
}
